﻿/* Project:         ApplicationList
 * Copyright:       Copyright (C) 2010, Rob Kennedy
 * Create Date:     2010-23-08 4:00 PM
 * Modified Date:   2010-23-08 4:00 PM
 * Comments:        2010-23-08 4:00 PM RK  Ignore SA1633
 *
 * This program is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>. 
 */
namespace ApplicationList
{
    using System;
    using System.Xml.Serialization;
    using CmdParamLib;

    /// <summary>
    /// The primary console program entry class
    /// </summary>
    class Program
    {
        #region Private Properties
        private static string remoteHostName = ".";
        private static string userName = string.Empty;
        private static string password = string.Empty;
        private static bool exportAsXml = false;
        #endregion

        #region Primary Method
        /// <summary>
        /// Contains the main entry for the console application
        /// </summary>
        /// <param name="args">Command line arguments</param>
        static void Main(string[] args)
        {
            bool render = true;

            foreach (CommandLineParameter param in CmdParamLib.CommandLine.Parameters)
            {
                switch (param.Name)
                {
                    case "help":
                    case "h":
                        render = false;
                        DisplayHelp();
                        break;
                    case "c":
                        remoteHostName = Convert.ToString(param.Value);
                        break;
                    case "u":
                        userName = Convert.ToString(param.Value);
                        break;
                    case "p":
                        password = Convert.ToString(param.Value);
                        break;
                    case "x":
                        exportAsXml = true;
                        break;
                }
            }

            if (render)
            {
                GetList();
            }
        }
        #endregion

        #region Private Methods
        /// <summary>
        /// Renders the application list content
        /// </summary>
        static void GetList()
        {
            ApplicationsList itemsList = new ApplicationsList(remoteHostName, userName, password);

            if (itemsList.ErrorMessage.Length == 0)
            {
                if (!exportAsXml)
                {
                    Console.WriteLine("\t Application Name\t Version");
                    foreach (ApplicationItem item in itemsList)
                    {
                        Console.WriteLine("\t{0}\t{1}", item.Name, item.Version);
                        Console.WriteLine("\tApplications");
                    }
                }
                else
                {
                    XmlSerializer serializer = new XmlSerializer(itemsList.GetType());
                    serializer.Serialize(Console.Out, itemsList);
                }
            }
            else
            {
                Console.WriteLine("Error: {0}", itemsList.ErrorMessage);
            }
        }

        /// <summary>
        /// Displays the console help menu screen
        /// </summary>
        static void DisplayHelp()
        {
            Console.WriteLine("{0} {1}\n{2}\n", ProgramInfo.Name, ProgramInfo.Version, ProgramInfo.Copyright);
            Console.WriteLine("{0} [options]\n", CommandLine.ExecutablePath);
            Console.WriteLine("Help\n" +
                              "----\n" +
                              "Option         Description\n" +
                              "--------------------------\n" +
                              "-h             Displays this help information\n" +
                              "-c [hostname]  Contains the computer host name to connect to. Default is '.'\n" +
                              "-u [username]  Sets the user name to authenticate with connecting to [hostname]\n" +
                              "-p [password]  Sets the password to authenticate with connecting to [hostname]\n" +
                              "-x             Outputs results in serialized XML format for parsing.\n");
        }
        #endregion
    }
}
